import rclpy
from rclpy.node import Node
from chapt4_interfaces.srv import FaceDetector
from ament_index_python.packages import get_package_share_directory
from cv_bridge import CvBridge
import cv2
import face_recognition
import time
from rcl_interfaces.msg import SetParametersResult

class FaceDetectorionNode(Node):
    def __init__(self, node_name):
        super().__init__(node_name)
        self.bridge = CvBridge()
        self.service = self.create_service(FaceDetector,'/face_detect',self.detect_face_callback)
        self.default_image_path = get_package_share_directory('demo_python_service')+'/resource/default.jpg'
        self.declare_parameter('face_locations_upsample_times',1)
        self.declare_parameter('face_locations_model','hog')
        self.upsample_times = self.get_parameter('face_locations_upsample_times').value
        self.model = self.get_parameter('face_locations_model').value
        self.add_on_set_parameters_callback(self.parameter_callback)
        pass

    def parameter_callback(self,parameters):
        for parameter in parameters:
            self.get_logger().info(f'参数{parameter.name}设置为：{parameter.value}')
            if parameter.name == 'face_locations_upsample_times':
                self.upsample_times = parameter.value
                pass
            if parameter.name == 'face_locations_model':
                self.model = parameter.value
                pass
            pass
        return SetParametersResult(successful=True)

    def detect_face_callback(self, request, response):
        if request.image.data:
            cv_image = self.bridge.imgmsg_to_cv2(request.image)
            pass
        else:
            cv_image = cv2.imread(self.default_image_path)
            pass
        start_time = time.time()
        self.get_logger().info('加载完图像，开始检测')
        face_locations = face_recognition.face_locations(cv_image,number_of_times_to_upsample=self.upsample_times,model=self.model)
        end_time = time.time()
        self.get_logger().info(f'检测完成，耗时{end_time-start_time}')
        response.number = len(face_locations)
        response.use_time = end_time-start_time
        for top,right,bottom,left in face_locations:
            response.top.append(top)
            response.right.append(right)
            response.bottom.append(bottom)
            response.left.append(left)
            pass
        return response
    pass

def main(args=None):
    rclpy.init(args=args)
    node = FaceDetectorionNode('face_detection_node')
    rclpy.spin(node)
    rclpy.shutdown()
    pass